package com.choosefine.springboot.devops.base.moudle.checkable;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * Created by 邱鹏 on 2016/8/23.
 */
@Controller
@RequestMapping("/wechat/check")
public class CheckEnableController {
    //这个Token是给微信开发者接入时填的
    //可以是任意英文字母或数字，长度为3-32字符
    private static String Token = "pjbao";
    Logger log = LoggerFactory.getLogger("微信/check");
    ObjectMapper mapper = new ObjectMapper();
    @Autowired
    CheckEnableService checkEnableService;

    @RequestMapping("/enable")
    public void doFilter(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, NoSuchFieldException {
        //微信服务器将发送GET请求到填写的URL上,这里需要判定是否为GET请求
        boolean isGet = request.getMethod().toLowerCase().equals("get");
        log.info("获得微信请求"+request.getMethod()+"\n" + mapper.writeValueAsString(request.getParameterMap()));
        if (isGet) {
            checkEnableService.checkEnable(request, response);
            //验证URL真实性
        } else {
            checkEnableService.receiveMsg(request, response);
            //处理接收消息
        }
    }
}
